package com.sxc.loglib;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.sxc.loglib.ui.LogActivity;

/**
 * Created by sunxunchao on 2020/1/13
 */
public class SensorHelper {

    private static final String TAG = SensorHelper.class.getSimpleName();

    private Sensor sensorAccelerometer;
    private SensorManager sm;
    private Context context;

    public SensorHelper(Context context) {
        this.context = context;
        sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        if (sm != null) {
            sensorAccelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (sensorAccelerometer != null) {
                sm.registerListener(sensorEventListener, sensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            }
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
//            Log.d(TAG, "onSensorChanged:" + "x=" + x + "y=" + y + "z=" + z);
            if ((Math.abs(x) + Math.abs(y) + Math.abs(z)) > 40) {
                LogActivity.startLogActivity(context);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
}
